package com.code.aspectj.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.code.aspectj.AppInterceptor;

/**
 * Created by RLZ
 * on 2022/1/6
 */
public class NetworkUtils {

    @SuppressLint("MissingPermission")
    public static boolean isConnected() {
        Context context = AppInterceptor.getContext();
        if (context == null) throw new NullPointerException("AppInterceptor notInitialized");

        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager == null) {
            return false;
        } else {
            NetworkInfo mobile = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            return mobile.isConnected() || wifi.isConnected();
        }
    }
}
